<?php

namespace app\base\controller\admin\miniapp;

use app\base\model\StyleModel;

use app\BaseController;



class Template extends BaseController{

    public function getStyle(){

        $style = StyleModel::find($this->request->shopId);

        if(null === $style){

            $return['style'] = [

                'main_color'=>'#00BA26',

                'back_img'=>null,

                'is_show_buy'=>0,

            ];

        }else{

            $datas = $style->toArray();

            $return['style'] = $datas['style'];

        }

        return $this->success($return);

    }

    public function saveStyle(){

        $main_color = $this->request->param('main_color');

        $back_img = $this->request->param('back_img');

        $is_show_buy= $this->request->param('is_show_buy/d',0);





        if(empty($main_color)) return $this->error('请选择主题颜色');

        if(empty($back_img)) return $this->error('请选择背景图');

        if(!in_array($is_show_buy,[1,0])) return $this->error('参数错误');

        $style = [

            "main_color"=>$main_color,

            'back_img'=>$back_img,

            'is_show_buy'=>$is_show_buy,

        ];

        if(null === $styleDatas = StyleModel::find($this->request->shopId)){

            $styleDatas = new StyleModel();

            $styleDatas->shop_id = $this->request->shopId;

        }

        $pre_back_img= $styleDatas->back_img ?? null;

        $styleDatas->style = $style;

        $styleDatas->save();

        cache('miniapp_style' . $this->request->shopId,null);

        imgReplaceAct($back_img,$pre_back_img);

        return $this->success('保存成功');

    }

}

